from flask_restful import Resource

from model.crud import QueryMultipleInstances
from model.tables import Blog
from restful.data import DataUid
from restful.response import ResponseSuccess
from restful.session import data

def _Posts(posts, is_reason=False):
    return [{
        'pid': i.id,
        **({'reason': i.reason} if is_reason else {})
    } for i in posts]

class UserPosts(Resource):
    @data(DataUid())
    def get(self,data):
        uncheck=QueryMultipleInstances(Blog,{'uid':data['uid'],'checked':False})
        unpass=QueryMultipleInstances(Blog,{'uid':data['uid'],'checked':True,'passed':False})
        passed=QueryMultipleInstances(Blog,{'uid':data['uid'],'checked':True,'passed':True})

        return ResponseSuccess(data={
            'uncheck':_Posts(uncheck),
            'unpass':_Posts(unpass,True),
            'pass':_Posts(passed)
        })